<template>
    <div class="app">
        <!--导航栏背景-->
        <div class="nav_bac">
            <!--导航功能按钮-->
            <div class="nav_but">
            </div>
            <div class="nav_left" style="float:right;background-color:#332B33;width: 100%;height: 60px">
                <el-menu router :default-active="this.$router.path" class="el-menu-demo" mode="horizontal"
                         @select="handleSelect" background-color="#332B33" text-color="#fff"
                         active-text-color="#ffd04b">
                    <!--酒店logo-->
                    <el-menu-item index="/">
                        <img style="border-radius: 25px;filter: invert(20%);margin-bottom: 8px" src="../assets/logo.jpg"
                             height="50" width="150"/>
                    </el-menu-item>
                    <el-menu-item index="/">房间预订</el-menu-item>
                    <el-menu-item @click="toOrder()">订单管理</el-menu-item>
                    <el-menu-item @click="test()">个人中心</el-menu-item>
                    <div v-if="token == null">
                        <el-button
                                style="float: right; color: #fff;margin-top: 12px;margin-left: 15px;margin-right: 10px"
                                type="text" @click="RegisterFormVisible = true">注册
                        </el-button>
                        <el-button style="float: right; color: #fff;margin-top: 12px;" type="text"
                                   @click="LoginFormVisible = true">登录
                        </el-button>
                    </div>
                    <div class="login_btn" style="float: right;color: #fff;" v-else>
                        <span style="font-size: 20px" class="">欢迎{{ token }} </span>
                        <el-button type="text" style="color: white;font-size: 20px" @click="exit()">退出</el-button>
                    </div>
                    <!--登录页面-->
                    <el-dialog title="登录界面" :append-to-body='true' :visible.sync="LoginFormVisible" width="30%" center>
                        <el-form :model="User" status-icon :rules="rules" ref="ruleForm">
                            <el-form-item label="用户账号：" prop="username" :label-width="formLabelWidth">
                                <el-input style="width: 300px;" v-model="User.username" autocomplete="off"></el-input>
                            </el-form-item>
                            <el-form-item label="用户密码：" prop="password" :label-width="formLabelWidth">
                                <el-input style="width: 300px" type="password" v-model="User.password"
                                          autocomplete="off"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer" style="margin-left: 50px">
                            <el-button style="width: 100px" @click="LoginFormVisible = false">取 消</el-button>
                            <el-button style="width: 100px" type="primary" @click="submitForm('ruleForm')">登 录
                            </el-button>
                        </div>
                    </el-dialog>
                    <!--注册页面-->
                    <el-dialog title="注册界面" :append-to-body='true' :visible.sync="RegisterFormVisible" width="30%"
                               center>
                        <el-form :model="People" status-icon :rules="rules" ref="ruleForm1" >
                            <el-form-item label="输入账号：" prop="username" :label-width="formLabelWidth">
                                <el-input style="width: 300px" v-model="People.username" autocomplete="off"></el-input>
                            </el-form-item>

                            <el-form-item label="输入密码：" prop="password" :label-width="formLabelWidth">
                                <el-input type="password" style="width: 300px" v-model="People.password" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer" style="margin-left: 50px">
                            <el-button style="width: 100px" @click="RegisterFormVisible = false">取 消</el-button>
                            <el-button style="width: 100px" type="primary" @click="RegisterForm('ruleForm1')">注 册</el-button>
                        </div>
                    </el-dialog>
                </el-menu>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Nav",
        data() {
            const checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    console.log(reg.test(value));
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };
            return {
                activeIndex: '1',
                activeIndex2: '1',
                token: localStorage.getItem("name"),
                User: {},
                People: {},
                LoginFormVisible: false,//登录页面
                RegisterFormVisible: false,//修改页面
                formLabelWidth: '100px',
                rules: {
                    username: [
                        {required: true, message: '请输入账号名称', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                }
            };
        },
        methods: {
            test() {
                if (!this.token) {
                    alert("请登录！")
                } else {
                    this.$router.push('/score');
                }

            },
            toOrder() {
                if (!this.token) {
                    alert("请登录！")
                } else {
                    this.$router.push('/order');
                }

            },
            handleSelect(key, keyPath) {
                console.log(key, keyPath);
            },
            /*退出*/
            exit() {
                alert("退出成功！")
                localStorage.removeItem("name")
                localStorage.removeItem("id")
                location.reload();
            },
            /*token*/
            findToken() {
                this.token = localStorage.getItem("name")
            },
            /*登录*/
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        if (!this.User.username) {
                            alert('请输入账号');
                            return false
                        }
                        if (!this.User.password) {
                            alert('请输入密码');
                            return false
                        }
                        let _this = this
                        this.$axios.post("http://localhost:8088/user/login", this.User).then(function (res) {
                            //清空本次保存数据
                            if (res.data != -1) {
                                localStorage.setItem("name", _this.User.username)
                                localStorage.setItem("id", res.data)
                                _this.token = localStorage.getItem("name");
                                _this.$options.methods.findToken();
                                alert("登录成功！")
                                _this.User = {};
                                _this.LoginFormVisible = false
                            } else {
                                alert("账号或密码错误！");
                                _this.User = {};
                            }
                        }).catch(function (err) {
                            localStorage.removeItem('name')
                            localStorage.removeItem('id')
                            localStorage.removeItem('RoleId')
                            alert("登陆失败！");
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            /*注册*/
            RegisterForm(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(!this.People.username){alert('请输入用户名称');return false}
                        if(!this.People.password){alert('请输入用户密码');return false}
                        this.People.roleId = 2
                        this.People.sex = 2
                        console.log(this.People)
                        let _this = this
                        this.$axios.post("http://localhost:8088/user/setUser",this.People).then(function () {
                            //清空本次保存数据
                            alert("注册成功！请重新登录！")
                            _this.People = {};
                            location. reload()
                        }).catch(function (err) {
                            alert("用户信息添加失败，请稍后再试")
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>
    .nav_bac {
        background-color: #545c64;
    }

    .nav_but {
        float: left;
    }

    .nav_menu {
        margin-left: 550px;
    }
</style>
